
/**
 **@author		周羽
 **@date  		2017-03-27
 **@brief		链表的创建以及插入等操作函数
 */
#include "linklist.h"

//创建双向循环链表，链表节点数为5
void loop_linklist_create(void)
{
	link_head = (loop_linklist_t *)malloc(sizeof(loop_linklist_t) * 5);
	bzero(link_head, 5 * sizeof(loop_linklist_t));


	link_head[0].next = &link_head[1];
	link_head[1].next = &link_head[2];
	link_head[2].next = &link_head[3];
	link_head[3].next = &link_head[4];
	link_head[4].next = &link_head[0];

	link_head[4].pre = &link_head[3];
	link_head[3].pre = &link_head[2];
	link_head[2].pre = &link_head[1];
	link_head[1].pre = &link_head[0];
	link_head[0].pre = &link_head[4];
}


//创建双向非循环链表
void undire_linklist_create(void)
{
	head_sql = (undire_linklist_t *)malloc(sizeof(undire_linklist_t));
	bzero(head_sql, sizeof(undire_linklist_t));

	end_sql = head_sql;
}


//判断双向非循环链表是否为空，空返回0，非空返回1
int empty_undire_linklist(void)
{
	return !(head_sql ==  end_sql);
}

//向循环链表中插入数据，第一次插入时，指针先指向下一个元素，在插入
//即指针总是指向最新插入的那个元素
void loop_head_insert(struct env_info msg)
{
	link_head = link_head->next;

	link_head->msg = msg;
}

//向双向非循环链表中向头部插入数据
void loop2undire_get(struct env_info msg)
{
	undire_linklist_t * linknode = (undire_linklist_t *)malloc(sizeof(undire_linklist_t));
	if(head_sql == end_sql)
	{
		linknode->msg = msg;

		head_sql->next = linknode;
		linknode->pre = head_sql;	

		end_sql = head_sql->next;
	}
	else
	{
		linknode->msg = msg;

		linknode->next = head_sql->next;
		linknode->next->pre = linknode;
		head_sql->next = linknode;
		linknode->pre = head_sql;
	}
	//link_head = link_head->pre;此处误动
}

//对双向非循环链表取出最后一个数据，此函数返回了该数据的地址
//用完数据时候要对该指针释放，free
undire_linklist_t * undire_end_get(void)
{
	undire_linklist_t * ret = end_sql;

	end_sql = end_sql->pre;

	return ret;
}

